/*******************************************************************************
 * File name: 		board.c
 * Description: 	board specyfic functions
 * Project: 		_lpc1788
 * Target: 			LPC1788
 * Compiler: 		arm-none-eabi-gcc
 * Date: 			04-07-2012
 * Author: 			Kuba
 * Based on: 		---
 ******************************************************************************/

/*==============================================================================
 Includes
==============================================================================*/
# include "LPC177x_8x.h"
# include "typedef.h"
# include "macros.h"
# include "gpio.h"
# include "board.h"
/*==============================================================================
 Defines
==============================================================================*/
# define SWITCH_DEBOUNCE	3000000

/*==============================================================================
 Types
==============================================================================*/

/*==============================================================================
 Globals
==============================================================================*/
static void (*uswitchService) (void);

/*==============================================================================
 Static function prototypes
==============================================================================*/
static void dummyService (void);

/*==============================================================================
 Global function definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:		boardLedInit
 description:		init led
 parameters:		port, led pin number. NOTE: pins start from 0.
 returned value:	port and pin number
------------------------------------------------------------------------------*/
struct io boardLedInit   (const enum gpioPort port, const uint32 pin)
{
	struct io tmp;

	switch (port)
	{
	case gpioPORT0:
		gpio32SetDir(gpioPORT0, (1 << pin), gpioOUTPUT);	/* set led port as output */
		gpio32SetValue(gpioPORT0, (1 << pin));					/* turn off led initially */
		tmp.port = gpioPORT0;
		break;

	case gpioPORT1:
		gpio32SetDir(gpioPORT1, (1 << pin), gpioOUTPUT);	/* set led port as output */
		gpio32SetValue(gpioPORT1, (1 << pin));					/* turn off led */
		tmp.port = gpioPORT1;
		break;

	case gpioPORT2:
		gpio32SetDir(gpioPORT2, (1 << pin), gpioOUTPUT);	/* set led port as output */
		gpio32SetValue(gpioPORT2, (1 << pin));					/* turn off led */
		tmp.port = gpioPORT2;
		break;

	case gpioPORT3:
		gpio32SetDir(gpioPORT3, (1 << pin), gpioOUTPUT);	/* set led port as output */
		gpio32SetValue(gpioPORT3, (1 << pin));					/* turn off led */
		tmp.port = gpioPORT3;
		break;

	case gpioPORT4:
		gpio32SetDir(gpioPORT4, (1 << pin), gpioOUTPUT);	/* set led port as output */
		gpio32SetValue(gpioPORT4, (1 << pin));				/* turn off led */
		tmp.port = gpioPORT4;
		break;

	case gpioPORT5:
		gpio32SetDir(gpioPORT5, (1 << pin), gpioOUTPUT);	/* set led port as output */
		gpio32SetValue(gpioPORT5, (1 << pin));					/* turn off led */
		tmp.port = gpioPORT5;
		break;

	default:
		tmp.port = gpioPORT0;
		break;
	}
	tmp.pin = (1 << pin);

	return tmp;
}
/*------------------------------------------------------------------------------
 function name:		boardLedOn
 description:		turn on led
 parameters:		port and number for led
 returned value:	none
------------------------------------------------------------------------------*/
void boardLedOn (const struct io led)
{
	gpio32ClearValue(led.port, led.pin);
}

/*------------------------------------------------------------------------------
 function name:		boardLedOn
 description:		turn off led
 parameters:		port and number for led
 returned value:	none
------------------------------------------------------------------------------*/
void boardLedOff (const struct io led)
{
	gpio32SetValue(led.port, led.pin);
}

/*------------------------------------------------------------------------------
 function name:		boardLedToggle
 description:		toggle led
 parameters:		port and number for led
 returned value:	none
------------------------------------------------------------------------------*/
void boardLedToggle (const struct io led)
{
	gpio32ToggleValue(led.port, led.pin);
}

/*------------------------------------------------------------------------------
 function name:		boardSwitchInit
 description:		init micro--switch
 parameters:		port, microswitch pin number
 returned value:	port and pin number
------------------------------------------------------------------------------*/
struct io boardSwitchInit (const enum gpioPort port, const uint32 pin)
{
	struct io tmp;

	switch (port)
	{
	case gpioPORT0:
		gpio32SetDir(gpioPORT0, (1 << pin), gpioINPUT);		/* set switch port as input */
		tmp.port = gpioPORT0;
		break;

	case gpioPORT1:
		gpio32SetDir(gpioPORT1, (1 << pin), gpioINPUT);		/* set led port as output */
		tmp.port = gpioPORT1;
		break;

	case gpioPORT2:
		gpio32SetDir(gpioPORT2, (1 << pin), gpioINPUT);		/* set led port as output */
		tmp.port = gpioPORT2;
		break;

	case gpioPORT3:
		gpio32SetDir(gpioPORT3, (1 << pin), gpioINPUT);		/* set led port as output */
		tmp.port = gpioPORT3;
		break;

	case gpioPORT4:
		gpio32SetDir(gpioPORT4, (1 << pin), gpioINPUT);		/* set led port as output */
		tmp.port = gpioPORT4;
		break;

	case gpioPORT5:
		gpio32SetDir(gpioPORT5, (1 << pin), gpioINPUT);		/* set led port as output */
		tmp.port = gpioPORT5;
		break;

	default:
		tmp.port = gpioPORT0;
		break;
	}
	tmp.pin = (1 << pin);

	return tmp;
}

/*------------------------------------------------------------------------------
 function name:		boardSwitchPressed
 description:		check if switch is pressed
 parameters:		uswitch
 returned value:	true or false
------------------------------------------------------------------------------*/
bool boardSwitchPressed (const struct io uswitch)
{
	volatile uint32 i;

	if (((gpio32ReadValue(uswitch.port)) & uswitch.pin) == 0)
	{
		for (i = 0; i < SWITCH_DEBOUNCE; ++i);	/* de-bouncing */
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

/*------------------------------------------------------------------------------
 function name:		boardSwitchIrqEnable
 description:		enable switch interrupt
 parameters:		uswitch, interrupt priority, user service- a function called when switch
 	 	 	 	 	 is pressed
 returned value:	true or false
------------------------------------------------------------------------------*/
bool boardSwitchIrqEnable (const struct io uswitch, const enum irqPriority priority, void (*const service)(void))
{
	/* check if service is not zero */
	if (service == 0)
	{
		uswitchService = dummyService;
	}
	else
	{
		uswitchService = service;
	}

	return (gpio32InitIrq(uswitch.port, BIT_POS(uswitch.pin), gpioFALLING_EDGE, priority, uswitchService));
}
/*==============================================================================
 Static function definitions
==============================================================================*/

/*------------------------------------------------------------------------------
 function name:		dummyService
 description:		only returns- to init function pointers
 parameters:		none
 returned value:	none
------------------------------------------------------------------------------*/
static void dummyService (void)
{
	return ;
}
/*==============================================================================
 Static ISR definitions
==============================================================================*/

/******************************************************************************
* END OF FILE
******************************************************************************/



